<!-- 小气候设备数据 -->
<template>
	<view class="page">
		<template v-if="token&&isLogin">
			<template v-if="loadingState.default">
				<loading :loadingState="loadingState"></loading>
			</template>
			<template v-else>
				<view class="body">
					<template v-if="detail">
						<view class="weather-area">
							<navigator :url="'/package_common/search_tool/search_tool?dev_type='+dev_type+'&typeid='+select_dev.typeid" hover-class="none" class="change-btn">
								<view class="select-data f-s30">{{select_dev.name}}</view>
								<view class="iconfont f-s20">&#xe623;</view>
							</navigator>
							<view class="area">
								<view class="icon">
									<image src="@/static/images/area_icon.png" mode="widthFix"></image>
								</view>{{newDevDetail.address||'--'}}
							</view>
							<view class="position-code">
								纬度：{{newDevDetail.latitude||'--'}}，经度：{{newDevDetail.longitude||'--'}}，海拔：{{newDevDetail.altitude||'--'}}
							</view>
							<view class="temperature-data">{{(newDevDetail.air_temperature||'-')+'℃'}}</view>
							<view class="data-wrap">
								<view class="item">
									<view class="icon">
										<image src="@/static/images/rang.png" mode="widthFix"></image>
									</view>
									<view class="info">
										<view class="val">{{(newDevDetail.rainfall||0)+'mm'}}</view>
										<view class="name">降雨量</view>
									</view>
								</view>
								<view class="item">
									<view class="icon">
										<image src="@/static/images/soli.png" mode="widthFix"></image>
									</view>
									<view class="info">
										<view class="val">{{(newDevDetail.air_humidity||'-')+'%'}}</view>
										<view class="name">空气湿度</view>
									</view>
								</view>
								<view class="item">
									<view class="icon">
										<image src="@/static/images/light.png" mode="widthFix"></image>
									</view>
									<view class="info">
										<view class="val">{{(newDevDetail.illuminance||'-')+'%'}}</view>
										<view class="name">光照度</view>
									</view>
								</view>
								<view class="item">
									<view class="icon">
										<image src="@/static/images/qiyl.png" mode="widthFix"></image>
									</view>
									<view class="info">
										<view class="val">{{(newDevDetail.soil_temperature||'-')+'℃'}}</view>
										<view class="name">土壤温度</view>
									</view>
								</view>
								<view class="item">
									<view class="icon">
										<image src="@/static/images/qiyl.png" mode="widthFix"></image>
									</view>
									<view class="info">
										<view class="val">{{(newDevDetail.soil_humidity||'-')+'%'}}</view>
										<view class="name">土壤湿度</view>
									</view>
								</view>
								<view class="item">
									<view class="icon">
										<image src="@/static/images/wine_speed.png" mode="widthFix"></image>
									</view>
									<view class="info">
										<view class="val">{{(newDevDetail.wind_speed||'-')+'m/s'}}</view>
										<view class="name">风速</view>
									</view>
								</view>
								<view class="item">
									<view class="icon">
										<image src="@/static/images/wine_direction.png" mode="widthFix"></image>
									</view>
									<view class="info">
										<view class="val">{{(newDevDetail.wind_direction||'-')+'Lus'}}</view>
										<view class="name">风向</view>
									</view>
								</view>
							</view>
						</view>

						<view class="container m-b2">
							<view class="date-tab">
								<view :class="['item day',{'active':params.time==1}]" @click="changeTime(1)">日</view>
								<view :class="['item week',{'active':params.time==2}]" @click="changeTime(2)">周</view>
								<view :class="['item month',{'active':params.time==3}]" @click="changeTime(3)">月</view>
							</view>

							
							<!-- 空气温度走势 -->
							<view class="data-area">
								<view class="page__title">
									<view v-if="params.time==1" class="title">一日气温走势</view>
									<view v-if="params.time==2" class="title">一周气温走势</view>
									<view v-if="params.time==3" class="title">一月气温走势</view>
								</view>
								<view class="data-chart">
									<template v-if="detail.temperatures&&detail.temperatures.air_temperature">
										<uni-ec-canvas class="uni-ec-canvas" ref="air_temperature"
											canvas-id="air_temperature" :ec="ec" :force-use-old-canvas="true">
										</uni-ec-canvas>
									</template>
									<template v-else>
										<view class="check-tips"><text>暂无统计数据</text></view>
									</template>
								</view>								
							</view>
							
							<!-- 空气湿度走势 -->
							<view class="data-area">
								<view class="page__title">
									<view v-if="params.time==1" class="title">一日空气湿度走势</view>
									<view v-if="params.time==2" class="title">一周空气湿度走势</view>
									<view v-if="params.time==3" class="title">一月空气湿度走势</view>
								</view>
								<template v-if="detail.humiditys&&detail.humiditys.air_humidity">
									<uni-ec-canvas class="uni-ec-canvas" ref="air_humidity"
										canvas-id="air_humidity" :ec="ec" :force-use-old-canvas="true">
									</uni-ec-canvas>
								</template>
								<template v-else>
									<view class="check-tips"><text>暂无统计数据</text></view>
								</template>
							</view>
							
							<!-- 土壤温度走势 -->
							<view class="data-area">
								<view class="page__title">
									<view v-if="params.time==1" class="title">一日土壤气温走势</view>
									<view v-if="params.time==2" class="title">一周土壤气温走势</view>
									<view v-if="params.time==3" class="title">一月土壤气温走势</view>
								</view>
								<template v-if="detail.temperatures&&detail.temperatures.soil_temperature">
									<uni-ec-canvas class="uni-ec-canvas" ref="soil_temperature"
										canvas-id="soil_temperature" :ec="ec" :force-use-old-canvas="true">
									</uni-ec-canvas>
								</template>
								<template v-else>
									<view class="check-tips"><text>暂无统计数据</text></view>
								</template>
							</view>
							
							<!-- 土壤湿度走势 -->
							<view class="data-area">
								<view class="page__title">
									<view v-if="params.time==1" class="title">一日土壤湿度走势</view>
									<view v-if="params.time==2" class="title">一周土壤湿度走势</view>
									<view v-if="params.time==3" class="title">一月土壤湿度走势</view>
								</view>
								<template v-if="detail.humiditys&&detail.humiditys.soil_humidity">
									<uni-ec-canvas class="uni-ec-canvas" ref="soil_humidity"
										canvas-id="soil_humidity" :ec="ec" :force-use-old-canvas="true">
									</uni-ec-canvas>
								</template>
								<template v-else>
									<view class="check-tips"><text>暂无统计数据</text></view>
								</template>
							</view>
							
							<!-- 降雨量走势 -->
							<view class="data-area">
								<view class="page__title">
									<view v-if="params.time==1" class="title">一日降雨量走势</view>
									<view v-if="params.time==2" class="title">一周降雨量走势</view>
									<view v-if="params.time==3" class="title">一月降雨量走势</view>
								</view>
								<template v-if="detail.rainfalls">
									<uni-ec-canvas class="uni-ec-canvas" ref="rainfalls" canvas-id="rainfalls"
										:ec="ec" :force-use-old-canvas="true">
									</uni-ec-canvas>
								</template>
								<template v-else>
									<view class="check-tips"><text>暂无统计数据</text></view>
								</template>
							</view>
							
							<!-- 风速走势 -->
							<view class="data-area">
								<view class="page__title">
									<view v-if="params.time==1" class="title">一日风速走势</view>
									<view v-if="params.time==2" class="title">一周风速走势</view>
									<view v-if="params.time==3" class="title">一月风速走势</view>
								</view>
								<template v-if="detail.wind_speeds">
									<uni-ec-canvas class="uni-ec-canvas" ref="wind_speeds"
										canvas-id="wind_speeds" :ec="ec" :force-use-old-canvas="true">
									</uni-ec-canvas>
								</template>
								<template v-else>
									<view class="check-tips"><text>暂无统计数据</text></view>
								</template>
							</view>
							
							<!-- 光照度 -->
							<view class="data-area">
								<view class="page__title">
									<view v-if="params.time==1" class="title">一日光照度走势</view>
									<view v-if="params.time==2" class="title">一周光照度走势</view>
									<view v-if="params.time==3" class="title">一月光照度走势</view>
								</view>
								<template v-if="detail.illuminances">
									<uni-ec-canvas class="uni-ec-canvas" ref="illuminances"
										canvas-id="illuminances" :ec="ec" :force-use-old-canvas="true">
									</uni-ec-canvas>
								</template>
								<template v-else>
									<view class="check-tips"><text>暂无统计数据</text></view>
								</template>
							</view>


						</view>
					</template>
					<template v-else>
						<empty msg="暂无数据"></empty>
					</template>
				</view>
			</template>
		</template>
	</view>
</template>

<script>
	import loading from '@/components/loading-more.vue'
	import empty from '@/components/empty.vue'
	import uniEcCanvas from '@/components/uni-ec-canvas.vue'
	import * as echarts from '@/common/echart/echarts.js'
	let chart = null
	export default {
		name: 'name',
		data() {
			return {
				token: uni.getStorageSync('token'),
				appid: getApp().globalData.appid,
				isLogin: false,
				userInfo: uni.getStorageSync('userInfo'),
				loginStateUpdate: false,
				loadingState: {
					default: true,
				},
				dataTabUpdate: {
					loading: true
				},
				id: '',
				dev_number: '',
				dev_type: '',
				date_type: '',
				detail: {},
				ec: {
					lazyLoad: true
				},
				echart_option: {},
				params: {
					time: 1
				},
				newDevDetail: {},
				select_dev:{
					id:'',
					name:'',
					typeid:'',
					update:false
				}
			}
		},
		async onLoad(params) {
			console.log(params)
			this.type = params.type;
			this.dev_number = params.dev_model;
			this.dev_type = params.dev_type;
			this.select_dev.name=params.dev_name;
			this.select_dev.id=params.dev_model;
			
			if(params.typeid){
				this.select_dev.typeid=params.typeid;
			}

			if (this.token) {
				this.isLogin = await this.$loginState('check');
				if (this.isLogin) {
					this.detail = await this.getXiaoQiHouYmd();
					if (this.detail) {
						this.newDevDetail = this.detail.newData;
						this.setChart();
					}
				} else {
					this.isLogin = await this.$loginState('reset');
				}
			} 

		},
		async onShow() {

			// 登录状态更新
			if (this.loginStateUpdate) {
				this.token = uni.getStorageSync('token');
				this.userInfo = uni.getStorageSync('userInfo');
				this.isLogin = true;
				this.detail = await this.getXiaoQiHouYmd();
				if (this.detail) {
					this.newDevDetail = this.detail.newData;
					this.setChart();
				}
				this.loginStateUpdate = false;
			}
			
			if(!this.token&&!this.isLogin){
				this.isLogin=await this.$loginState('reset'); 
			}
			
			// 设备切换
			if (this.select_dev.update) {
				this.dataInit(); 
				this.detail = await this.getXiaoQiHouYmd();
				if (this.detail) {
					this.newDevDetail = this.detail.newData;
					this.setChart();
				}
				this.select_dev.update = false;
			}




		},
		onReachBottom() {

		},
		methods: {

			dataInit: function() {
				this.loadingState.default = true;
				this.dataTabUpdate.loading = true;
			},

			// 获取小气候数据
			getXiaoQiHouYmd: async function() {
				let res = await this.$api.tool_api.getXiaoQiHouYmd({
					appid: this.appid,
					token: this.token,
					user_id: this.userInfo.uid,
					page: 1,
					limit: 10,
					device_num: this.dev_number,
					start_date: '',
					over_date: '',
					time: this.params.time,
				})
				if (res.data.ret == 1000) {
					this.loadingState.default = false;
					this.dataTabUpdate.loading = false;
					return res.data.data
				} else {
					this.loadingState.default = false;
					this.dataTabUpdate.loading = false;
				}
			},

			changeTime: async function(type) {
				if (this.params.time == type) {
					return
				} else {
					this.dataInit();
					this.params.time = type;
					this.dataTabUpdate.default = true;
					this.detail = await this.getXiaoQiHouYmd();
					if (this.detail) {
						this.newDevDetail = this.detail.newData;
						this.setChart();
					}
				}
			},

			// 降雨量
			initRainfallsChart(canvas, width, height, canvasDpr) {
				chart = echarts.init(canvas, null, {
					width: width,
					height: height,
					devicePixelRatio: canvasDpr
				})
				canvas.setChart(chart)
				chart.setOption(this.echart_option.rainfalls)
				return chart
			},

			// 空气温度
			initAirTemperatureChart(canvas, width, height, canvasDpr) {
				chart = echarts.init(canvas, null, {
					width: width,
					height: height,
					devicePixelRatio: canvasDpr
				})
				canvas.setChart(chart)
				chart.setOption(this.echart_option.air_temperature)
				return chart
			},

			// 空气湿度
			initAirHumidityChart(canvas, width, height, canvasDpr) {
				chart = echarts.init(canvas, null, {
					width: width,
					height: height,
					devicePixelRatio: canvasDpr
				})
				canvas.setChart(chart)
				chart.setOption(this.echart_option.air_humidity)
				return chart
			},

			// 空气温度
			initSoilTemperatureChart(canvas, width, height, canvasDpr) {
				chart = echarts.init(canvas, null, {
					width: width,
					height: height,
					devicePixelRatio: canvasDpr
				})
				canvas.setChart(chart)
				chart.setOption(this.echart_option.soil_temperature)
				return chart
			},

			// 土壤湿度
			initSoilHumidityChart(canvas, width, height, canvasDpr) {
				chart = echarts.init(canvas, null, {
					width: width,
					height: height,
					devicePixelRatio: canvasDpr
				})
				canvas.setChart(chart)
				chart.setOption(this.echart_option.soil_humidity)
				return chart
			},

			// 风速
			initWindSpeedsChart(canvas, width, height, canvasDpr) {
				chart = echarts.init(canvas, null, {
					width: width,
					height: height,
					devicePixelRatio: canvasDpr
				})
				canvas.setChart(chart)
				chart.setOption(this.echart_option.wind_speeds)
				return chart
			},

			// 光照度
			initIlluminancesChart(canvas, width, height, canvasDpr) {
				chart = echarts.init(canvas, null, {
					width: width,
					height: height,
					devicePixelRatio: canvasDpr
				})
				canvas.setChart(chart)
				chart.setOption(this.echart_option.illuminances)
				return chart
			},

			// 设置图表
			setChart: function() {
				if (this.detail.temperatures && this.detail.temperatures.air_temperature) {
					this.echart_option.air_temperature = this.setEchartOption(this.detail.date, this.detail
						.temperatures
						.air_temperature); // 空气温度echart配置
				}

				if (this.detail.humiditys && this.detail.humiditys.air_humidity) {
					this.echart_option.air_humidity = this.setEchartOption(this.detail.date, this.detail.humiditys
						.air_humidity); // 空气湿度echart配置
				}

				if (this.detail.temperatures && this.detail.temperatures.soil_temperature) {
					this.echart_option.soil_temperature = this.setEchartOption(this.detail.date, this.detail
						.temperatures
						.soil_temperature); // 土壤温度echart配置
				}

				if (this.detail.humiditys && this.detail.humiditys.soil_humidity) {
					this.echart_option.soil_humidity = this.setEchartOption(this.detail.date, this.detail.humiditys
						.soil_humidity); // 土壤湿度echart配置
				}

				if (this.detail.wind_speeds) {
					this.echart_option.wind_speeds = this.setEchartOption(this.detail.date, this.detail
						.wind_speeds); // 风速echart配置
				}

				if (this.detail.illuminances) {
					this.echart_option.illuminances = this.setEchartOption(this.detail.date, this.detail
						.illuminances); // 光照度	
				}

				if (this.detail.rainfalls) {
					this.echart_option.rainfalls = this.setEchartOption(this.detail.date, this.detail
						.rainfalls); // 降雨量echart配置			
				}


				setTimeout(() => {
					if (this.detail.temperatures && this.detail.temperatures.air_temperature) {
						this.$refs.air_temperature.init(this.initAirTemperatureChart)
					}

					if (this.detail.humiditys && this.detail.humiditys.air_humidity) {
						this.$refs.air_humidity.init(this.initAirHumidityChart)
					}

					if (this.detail.temperatures && this.detail.temperatures.soil_temperature) {
						this.$refs.soil_temperature.init(this.initSoilTemperatureChart)
					}

					if (this.detail.humiditys && this.detail.humiditys.soil_humidity) {
						this.$refs.soil_humidity.init(this.initSoilHumidityChart)
					}

					if (this.detail.wind_speeds) {
						this.$refs.wind_speeds.init(this.initWindSpeedsChart)
					}

					if (this.detail.illuminances) {
						this.$refs.rainfalls.init(this.initRainfallsChart)
					}

					if (this.detail.rainfalls) {
						this.$refs.illuminances.init(this.initIlluminancesChart)
					}
				}, 300)
			},

			// 设置图表配置
			setEchartOption: function(xdata, ydata) {
				console.log(xdata)
				let interval = 0;
				if (xdata.length > 16) {
					interval = 1
				}
				let option = {
					grid: {
						x: 40,
						y: 20,
						x2: 20,
						y2: 40,
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'line',
						}
					},
					xAxis: {
						type: 'category',
						data: xdata,
						axisLabel: {
							show: true,
							textStyle: {
								color: '#000'
							},
							interval: interval,
							rotate: 45, //倾斜度 -90 至 90 默认为0
						},
						axisLine: {
							lineStyle: {
								type: 'solid',
								color: '#000', //边线的颜色
								width: '1' //坐标线的宽度
							}
						},
					},
					yAxis: {
						type: 'value',
						axisLabel: {
							show: true,
							textStyle: {
								color: '#000',
							}
						},
						splitLine: {
							show: false
						},
						axisLine: {
							lineStyle: {
								type: 'solid',
								color: '#000', //边线的颜色
								width: '1' //坐标线的宽度
							}
						},
					},
					series: [{
						name: '',
						type: 'line',
						itemStyle: {
							normal: {
								lineStyle: {
									color: '#09f',
									width: 1
								}
							}
						},
						smooth: true,
						data: ydata
					}]
				}

				return option
			}

		},
		components: {
			loading,
			empty,
			uniEcCanvas
		},
		computed: {

		},
		watch: {


		}

	}
</script>

<style lang="less">
	.page__title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 30rpx;

		.title {
			font-size: 32rpx;
			font-weight: bold;
		}
	}

	.data-area {
		// height: 220px;
		margin-bottom: 20rpx;
		background: #fff;
		.data-chart{
			height:450rpx;
			.loading-box{
				height:100%;
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.check-tips {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100%;
				font-size: 28rpx;
				color: #666;
			}
		}
	}

	.weather-area {
		height: 780rpx;
		background: url(../../static/images/weather_bg.png) no-repeat;
		background-size: cover;
	}
	
	.change-btn{
		color:#fff;
		.iconfont{
			color:#fff
		}
	}

	.area {
		display: flex;
		justify-content: center;
		padding: 30rpx 0 10rpx;
		font-size: 30rpx;
		color: #fff;

		.icon {
			width: 30rpx;
			height: 30rpx;
			margin-right: 20rpx;

			image {
				width: 100%;
			}
		}
	}

	.position-code {
		margin-bottom: 40rpx;
		text-align: center;
		color: #fff;
		font-size: 24rpx;
	}

	.temperature-data {
		margin-bottom: 50rpx;
		text-align: center;
		font-size: 46rpx;
		font-weight: bold;
		color: #fff;
	}

	.data-wrap {
		display: flex;
		flex-wrap: wrap;
		margin: 0 10rpx;

		.item {
			display: flex;
			width: 30.3%;
			padding-left: 3%;
			margin-bottom: 50rpx;

			.icon {
				display: flex;
				flex-shrink: 0;
				justify-content: center;
				align-items: center;
				width: 80rpx;
				height: 80rpx;
				margin-right: 10rpx;

				image {
					width: 70%
				}
			}

			.info {
				color: #fff;
				font-size: 28rpx;
			}
		}
	}

	.date-tab {
		display: flex;
		width: 250rpx;
		margin: 20rpx auto;
		height: 50rpx;
		line-height: 50rpx;
		border-radius: 15rpx;
		border: 1rpx solid #1199FE;
	}

	.date-tab .item {
		flex-grow: 1;
		flex-shrink: 0;
		width: 1%;
		text-align: center;
		font-size: 28rpx;
		color: #1199FE;
	}

	.date-tab .month.active {
		background: #1199FE;
		border-top-right-radius: 13rpx;
		border-bottom-right-radius: 13rpx;
		color: #fff;
	}

	.date-tab .week.active {
		background: #1199FE;
		color: #fff;
	}

	.date-tab .day.active {
		background: #1199FE;
		border-top-left-radius: 13rpx;
		border-bottom-left-radius: 13rpx;
		color: #fff;
	}

	.canvas-area {
		margin: 0 20rpx;
	}

	.check-tips {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 450rpx;
		font-size: 28rpx;
		color: #666;
	}
</style>
